package csse3004.GD.SysAdmin;

/**
 * @author s4201567 Tony Yu
 */
import csse3004.GD.DA.SysAdminDA;
import csse3004.GD.PollAdminAccount;
import java.util.ArrayList;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Tony Yu s4201567
 */
public class ManagePAController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());
    private SysAdminService sds = new SysAdminDA();

    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        ArrayList<PollAdminAccount> adminList = new ArrayList<PollAdminAccount>();
        adminList = sds.getPollAdminList();
        int activeList[] = sds.displayActiveAdmin();
        
        if (hsr.getParameter("pid") != null) {
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            logger.info("pid: " + pid);
            sds.deactivateAccount(pid);
            //DB function to deactivate selected poll admin account

            String deactive = "The account has successfully been deactivated";
            logger.info("Deactive:" + deactive);

            return new ModelAndView("successMsg", "deactive", deactive);
        } 

        ModelAndView mnv = new ModelAndView("managePA", "admin", adminList);
        mnv.addObject("active", activeList);
        return mnv;  
    }
}
    
